package com.xmtlxx.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.system.domain.bo.SysCheckTypeBo;
import com.xmtlxx.system.domain.vo.SysCheckTypeVo;
import com.xmtlxx.system.service.ISysCheckTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 检查项类型Controller
 *
 * @author xmtlxx
 * @date 2022-04-26
 */
@Validated
@Api(value = "检查项类型控制器", tags = {"检查项类型管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/sysCheckType")
public class SysCheckTypeController extends BaseController {

    private final ISysCheckTypeService iSysCheckTypeService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<SysCheckTypeVo> initBean(){
        SysCheckTypeVo vo = new SysCheckTypeVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 获取检查项类型下拉树列表
     */
    @ApiOperation("获取检查项类型下拉树列表")
    @SaCheckPermission("system:sysCheckType:list")
    @GetMapping("/treeselect")
    public R<List<Tree<Long>>> treeselect(SysCheckTypeBo bo) {
        List<SysCheckTypeVo> types = iSysCheckTypeService.queryList(bo);
        return R.ok(iSysCheckTypeService.buildTreeSelect(types));
    }

    /**
     * 查询检查项类型列表
     */
    @ApiOperation("查询检查项类型列表")
    @SaCheckPermission("system:sysCheckType:list")
    @GetMapping("/list")
    public R<List<SysCheckTypeVo>> list(SysCheckTypeBo bo) {
        List<SysCheckTypeVo> list = iSysCheckTypeService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 查询部门列表（排除节点）
     */
    @ApiOperation("查询部门列表（排除节点）")
    @SaCheckPermission("system:sysCheckType:list")
    @GetMapping("/list/exclude/{typeId}")
    public R<List<SysCheckTypeVo>> excludeChild(@ApiParam("类型ID") @PathVariable(value = "typeId", required = false) Long typeId) {
        List<SysCheckTypeVo> list = iSysCheckTypeService.queryList(new SysCheckTypeBo());
        list.removeIf(d -> d.getTypeId().equals(typeId));
        return R.ok(list);
    }


    /**
     * 导出检查项类型列表
     */
    @ApiOperation("导出检查项类型列表")
    @SaCheckPermission("system:sysCheckType:export")
    @Log(title = "检查项类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated SysCheckTypeBo bo, HttpServletResponse response) {
        List<SysCheckTypeVo> list = iSysCheckTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "检查项类型", SysCheckTypeVo.class, response);
    }

    /**
     * 获取检查项类型详细信息
     */
    @ApiOperation("获取检查项类型详细信息")
    @SaCheckPermission("system:sysCheckType:query")
    @GetMapping("/{typeId}")
    public R<SysCheckTypeVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("typeId") Long typeId) {
        return R.ok(iSysCheckTypeService.queryById(typeId));
    }

    /**
     * 新增检查项类型
     */
    @ApiOperation("新增检查项类型")
    @SaCheckPermission("system:sysCheckType:add")
    @Log(title = "检查项类型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysCheckTypeVo vo) {
        SysCheckTypeBo bo = new SysCheckTypeBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysCheckTypeService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改检查项类型
     */
    @ApiOperation("修改检查项类型")
    @SaCheckPermission("system:sysCheckType:edit")
    @Log(title = "检查项类型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysCheckTypeVo vo) {
        SysCheckTypeBo bo = new SysCheckTypeBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysCheckTypeService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除检查项类型
     */
    @ApiOperation("删除检查项类型")
    @SaCheckPermission("system:sysCheckType:remove")
    @Log(title = "检查项类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeIds}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] typeIds) {
        return toAjax(iSysCheckTypeService.deleteWithValidByIds(Arrays.asList(typeIds), true) ? 1 : 0);
    }
}
